package com.innovation.util;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.util.CharsetUtil;

import com.innovation.message.PBMessageProto.PBMessage;

public class NettyUtil {
	/**
	 * 发送http响应数据(发送完关闭连接)
	 * @param contentType
	 * @param channel
	 * @param content
	 */
	public static void sendHttpResponse(String contentType, Channel channel, String content){
		FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
		response.headers().set(CONTENT_TYPE, contentType);
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        channel.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
	
	/**
	 * 发送数据给客户端
	 * @param channel
	 * @param message
	 */
	public static ChannelFuture sendMessage2Client(Channel channel, PBMessage message){
		if(channel == null || !channel.isOpen()){
			return null;
		}
		ByteBuf buf = Unpooled.buffer(message.getSerializedSize());
		buf.writeBytes(message.toByteArray());
		return channel.writeAndFlush(new BinaryWebSocketFrame(buf));
	}
}
